<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @keyframes run {
            from {
                transform: translateX(0px);
            }

            to {
                transform: translateX(300px);
            }
        }

        .ani {
            width: 50px;
            height: 50px;
            border-radius: 25px;
            background: red;
            animation: run 500ms linear infinite alternate;
        }

        .btn-list {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: row;
        }

        .btn {
            width: 100px;
            height: 100px;
            background: pink;
            color: white;
            border-radius: 50%;
            font-size: 18px;
            display: flex;
            justify-content: center;
            align-items: center;
            justify-self: center;
        }

        .item {
            width: 100%;
            height: 80px;
            color: white;
            font-size: 30px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .item-red {
            background: red;
        }

        .item-blue {
            background: blue;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <!-- <div class="ani"></div> -->
    <div class="btn-list">
        <div id="normal" class="btn">normal</div>
        <div id="optimize" class="btn">optimize</div>
    </div>
    <div id="app"> </div>
    <div class="item item-blue"> 1</div>
    <div class="item item-red"> 2</div>

    <div class="item item-blue"> 3</div>
    <div class="item item-red"> 4</div>

    <div class="item item-blue"> 5</div>
    <div class="item item-red"> 6</div>

    <div class="item item-blue"> 7</div>
    <div class="item item-red"> 8</div>

    <div class="item item-blue"> 9</div>
    <div class="item item-red"> 10</div>

    <div class="item item-blue"> 11</div>
    <div class="item item-red"> 12</div>

    <div class="item item-blue"> 13</div>
    <div class="item item-red"> 14</div>

    <div class="item item-blue"> 15</div>
    <div class="item item-red"> 16</div>
    <script type="module">
        import { idleCallback, idle } from "/run-time-opti/dist/es/index.js"
        console.log('test idle', idle);
        console.log('test idleCallback', idleCallback);
        let count = 10;
        let curCount = 0;
        const test = () => {
            let str = 0;
            for (let i = 0; i < 1000000; i++) {
                str += i
            }
        }

        const p1 = (res) => {
            console.log("test p1 res", res, 1111, this);
            curCount = 0;
            test();
            return 1111;
        };
        const p2 = (res) => {
            curCount = 0;
            test();
            console.log("test p2 res", 22222);
        }
        const p3 = (res) =>
            new Promise((resolve) => {
                console.log("test p3 res", res, 3333333);
                curCount = 0;
                test();
                setTimeout(() => {
                    resolve(33333);
                }, 5000)

            });
        const p4 = (res) =>
            new Promise((resolve, reject) => {
                console.log("test p4 res", res, 4444444);
                curCount = 0;
                test();
                setTimeout(() => {
                    resolve(4444);
                }, 5000)
            });
        const p5 = (res) =>
            new Promise((resolve) => {
                console.log("test p5 res", res, 5555555);
                curCount = 0;
                test();
                resolve(5555);

            });
        const p6 = (res) =>
            new Promise((resolve) => {
                console.log("test p6 res", res, 666666);
                curCount = 0;
                test();
                resolve(6666);
            });
        const onClick = (opti = false) => {
            try {
                p1();
                idle([
                {
                    key: 'p2',
                    task: p2
                }],100).then(res => {
                    console.log('test idle:', res)
                })
                p3()
                idleCallback((params)=>{
                    console.log('test idleCallback params', params)
                }, { timeout: 100})
                p5();

            } catch (error) {
                console.log('test error:', error);
            }
        }
        const normal = document.getElementById("normal")
        normal.addEventListener('click', () => onClick())

        const optimize = document.getElementById("optimize")
        optimize.addEventListener('click', () => onClick(true))
    </script>
</body>

</html>